package com.imooc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域
 * @author gcq
 * @Create 2022-01-05
 */
@Configuration
public class CorsConfig
{


    public CorsConfig()
    {

    }

    @Bean
    public CorsFilter corsFilter()
    {
        CorsConfiguration config = new CorsConfiguration();
        // 请求地址
        config.addAllowedOrigin("http://localhost:8080");
        config.addAllowedOrigin("http://center.z.mukewang.com:8080");
        config.addAllowedOrigin("http://shop.z.mukewang.com:8080");
        config.addAllowedOrigin("http://center.z.mukewang.com");
        config.addAllowedOrigin("http://shop.z.mukewang.com");
        // 是否发送cookie信息【支持用户凭据】
        config.setAllowCredentials(true);
        // 允许请求的hedaer
        config.addAllowedHeader("*");
        // 允许请求的方式
        config.addAllowedMethod("*");

        // 为url添加映射路径
        UrlBasedCorsConfigurationSource corsSource = new UrlBasedCorsConfigurationSource();
        corsSource.registerCorsConfiguration("/**", config);

        // 返回定义好的 corsSource
        return new CorsFilter(corsSource);
    }
}